function add() {
	/* 返回的是一个validate对象，这个对象有一个form方法，返回的是是否通过验证 */
	var validate = $('#form1').validate({
		rules : {
			name: {required: true, maxlength: 8},
			description: {maxlength: 10},
			permission: {required: true, letterMore: true, maxlength: 30},
			enabled: "required"
		},
		messages: {
			name: {required: "必填字段!", maxlength: "最多可以输入{0}个字符!"},
			description: {maxlength: "最多可以输入{0}个字符!"},
			permission: {required: "必填字段!", letterMore: "只允许输入字母、数字、冒号、下划线" ,maxlength: "最多可以输入{0}个字符!"},
			enabled: "必填字段!"
		},
		highlight : function(element) {
			//				$(element).parent('td').find('label').before('<br>');
		},
		success : function(element) {
		}
	});
	$('#form1').submit();
}

$(function() {
	//返回按钮
	$('#btnBack').on({
		  click: function() {
			  $("#backForm").submit();
		  }
	});
	//字符、数字、分号、下划线
	$.validator.addMethod('letterMore', function(value, element) {
		var regex = /^[a-zA-Z0-9:_]*$/;
		return regex.test(value);
	});
})

$(document).ready(function() {
	var rolePage = $("input[name=rolePage]").val();
	var roleRows = $("input[name=roleRows]").val();
	var roleTotal = $("#roleTotal").val();
	$('ul#rolePagination').page({
		"curPage": parseInt(rolePage),
		"pageSize": parseInt(roleRows),
		"totalCount": parseInt(roleTotal),
		"init": true,
		"callback": function (page) {
			query(page);
		}
	});

	 $('#queryRole').on('click', function() {
			$.ajax({
				type:'post',
				url:'/role/data',
				data:{
					page:1,
					rows:100,
					name:$('div#addRole #name').val(),
					description:$('div#addRole #description').val(),
					enabled:$('div#addRole #enabled').val()
				},
				success:function(data) {
					$('#roleTable').empty();
					var rows = data.rows;
					for(var i = 0; i < rows.length; i++) {
						var p = rows[i];
						var tr = $('<tr/>');
						if (i%2 == 1) {
							tr = $('<tr bgcolor="#f2f2f2">');
						}
						$('<td align="center"/>').append('<input type="checkbox" value="'+ p.id +'"/>').append(i+1).appendTo(tr);
						$('<td/>').text($.trim(p.name)).appendTo(tr);
						$('<td/>').text($.trim(p.description)).appendTo(tr);
						if (p.enabled == 1) {
							$('<td/>').text('启用').appendTo(tr);
						} else {
							$('<td/>').text('禁用').appendTo(tr);
						}
						tr.appendTo($('#roleTable'));
					}
				}
			});
		});
	 
		//选择产品线
		$("#roleTable").on('click', 'tr', function(){
			var checkbox = $(this).children("td").children("input[type='checkbox']");
			checkbox.click();
		});
		
		$("#roleTable").on('click', 'tr td input', function(event){
			//阻止冒泡事件
			event.stopPropagation();
		});
		
		//已经选择的产品线
		$("#roleListTable tr").on('click', function(){
			var checkbox = $(this).children("td").children("input[type='checkbox']");
			checkbox.click();
		});
		
		$("#roleListTable tr td input").on('click', function(event){
			//阻止冒泡事件
			event.stopPropagation();
		});
		
		//全选/反选按钮,已经存在的列表
		$('#checkRoleListAll').on(
			  'click',function() {
					if ($('#checkRoleListAll').is(':checked')) {
						 $("#roleListTable").find("input[type='checkbox']:visible").each(function () {
							 //只对可见的操作
							$(this).prop("checked", "checked");
							
						});
					}else {
						 $("#roleListTable").find("input[type='checkbox']:visible").each(function () {
							 //只对可见的操作
							$(this).removeAttr("checked");
						});
					}
			  }
		);

		//全选/反选按钮 查询列表
		$('#selectRoleListAll').on(
			  'click',function() {
					if ($('#selectRoleListAll').is(':checked')) {
						 $("#roleTable").find("input[type='checkbox']:visible").each(function () {
							 //只对可见的操作
							$(this).prop("checked", "checked");
							
						});
					}else {
						 $("#roleTable").find("input[type='checkbox']:visible").each(function () {
							 //只对可见的操作
							$(this).removeAttr("checked");
						});
					}
			  }
		);
	 
	 $('#resetRole').on('click', function() {
		 $('div#addRole input:text').val('');
		 $('div#addRole #enabled').val('');
	});
	 
	 $('#batchSave').on('click', function() {
		 var checked = $('#roleTable input:checkbox:checked');
			if (checked.length == 0) {
				alert('请选择角色!');
				return;
			}
			var ids = "";
			for(var i = 0; i < checked.length; i++) {
				ids += $(checked[i]).val() + ";";
			}
		 
			$.ajax({
    			type:'post',
    			url:'/roleAuthoritiesRelation/saveBatchRoleForAuth',
    			data:{authId:$("#authId").val(),roleIds:ids},
    			success:function(data) {
    				if (!data.success) {
    					alert(data.reason);
    				} else {
    					$('#addRole').modal('hide');
    					$('#tabForm input[name=tab]').val('roles');
    					$('#tabForm').submit();
    				}
    			}
    		});
	});
	 
	 $('#btnRoleRm').on('click', function() {
		 var checked = $('#roleListTable input:checkbox:checked');
			if (checked.length == 0) {
				alert('请选择一条数据!');
				return;
			}
			var ids = "";
			for(var i = 0; i < checked.length; i++) {
				ids += $(checked[i]).val() + ",";
			}
			$.ajax({
    			type:'post',
    			url:'/roleAuthoritiesRelation/deleteForAuth',
    			data:{authId:$("#authId").val(),roleIds:ids},
    			success:function(data) {
    				if (!data.success) {
    					alert(data.reason);
    				} else {
    					$('#tabForm input[name=tab]').val('roles');
    					$('#tabForm').submit();
    				}
    			}
    		});
		});
});

function query(cp) {
	$('#tabForm input:hidden[name=tab]').val('roles');
	$('#tabForm input:hidden[name=rolePage]').val(cp);
	$('#tabForm input:hidden[name=roleRows]').val(10);
	$('#tabForm').submit();
}